/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Denomination;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Monitoring;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class DenominationJpaController {

    public DenominationJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Denomination denomination) {
        if (denomination.getMonitoringList() == null) {
            denomination.setMonitoringList(new ArrayList<Monitoring>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Monitoring> attachedMonitoringList = new ArrayList<Monitoring>();
            for (Monitoring monitoringListMonitoringToAttach : denomination.getMonitoringList()) {
                monitoringListMonitoringToAttach = em.getReference(monitoringListMonitoringToAttach.getClass(), monitoringListMonitoringToAttach.getIdmonitoring());
                attachedMonitoringList.add(monitoringListMonitoringToAttach);
            }
            denomination.setMonitoringList(attachedMonitoringList);
            em.persist(denomination);
            for (Monitoring monitoringListMonitoring : denomination.getMonitoringList()) {
                Denomination oldDenominationOfMonitoringListMonitoring = monitoringListMonitoring.getDenomination();
                monitoringListMonitoring.setDenomination(denomination);
                monitoringListMonitoring = em.merge(monitoringListMonitoring);
                if (oldDenominationOfMonitoringListMonitoring != null) {
                    oldDenominationOfMonitoringListMonitoring.getMonitoringList().remove(monitoringListMonitoring);
                    oldDenominationOfMonitoringListMonitoring = em.merge(oldDenominationOfMonitoringListMonitoring);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Denomination denomination) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Denomination persistentDenomination = em.find(Denomination.class, denomination.getIddenomination());
            List<Monitoring> monitoringListOld = persistentDenomination.getMonitoringList();
            List<Monitoring> monitoringListNew = denomination.getMonitoringList();
            List<Monitoring> attachedMonitoringListNew = new ArrayList<Monitoring>();
            for (Monitoring monitoringListNewMonitoringToAttach : monitoringListNew) {
                monitoringListNewMonitoringToAttach = em.getReference(monitoringListNewMonitoringToAttach.getClass(), monitoringListNewMonitoringToAttach.getIdmonitoring());
                attachedMonitoringListNew.add(monitoringListNewMonitoringToAttach);
            }
            monitoringListNew = attachedMonitoringListNew;
            denomination.setMonitoringList(monitoringListNew);
            denomination = em.merge(denomination);
            for (Monitoring monitoringListOldMonitoring : monitoringListOld) {
                if (!monitoringListNew.contains(monitoringListOldMonitoring)) {
                    monitoringListOldMonitoring.setDenomination(null);
                    monitoringListOldMonitoring = em.merge(monitoringListOldMonitoring);
                }
            }
            for (Monitoring monitoringListNewMonitoring : monitoringListNew) {
                if (!monitoringListOld.contains(monitoringListNewMonitoring)) {
                    Denomination oldDenominationOfMonitoringListNewMonitoring = monitoringListNewMonitoring.getDenomination();
                    monitoringListNewMonitoring.setDenomination(denomination);
                    monitoringListNewMonitoring = em.merge(monitoringListNewMonitoring);
                    if (oldDenominationOfMonitoringListNewMonitoring != null && !oldDenominationOfMonitoringListNewMonitoring.equals(denomination)) {
                        oldDenominationOfMonitoringListNewMonitoring.getMonitoringList().remove(monitoringListNewMonitoring);
                        oldDenominationOfMonitoringListNewMonitoring = em.merge(oldDenominationOfMonitoringListNewMonitoring);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = denomination.getIddenomination();
                if (findDenomination(id) == null) {
                    throw new NonexistentEntityException("The denomination with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Denomination denomination;
            try {
                denomination = em.getReference(Denomination.class, id);
                denomination.getIddenomination();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The denomination with id " + id + " no longer exists.", enfe);
            }
            List<Monitoring> monitoringList = denomination.getMonitoringList();
            for (Monitoring monitoringListMonitoring : monitoringList) {
                monitoringListMonitoring.setDenomination(null);
                monitoringListMonitoring = em.merge(monitoringListMonitoring);
            }
            em.remove(denomination);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Denomination> findDenominationEntities() {
        return findDenominationEntities(true, -1, -1);
    }

    public List<Denomination> findDenominationEntities(int maxResults, int firstResult) {
        return findDenominationEntities(false, maxResults, firstResult);
    }

    private List<Denomination> findDenominationEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Denomination.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Denomination findDenomination(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Denomination.class, id);
        } finally {
            em.close();
        }
    }

    public int getDenominationCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Denomination> rt = cq.from(Denomination.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
